'use strict';

const Service = require('egg').Service;

class BorrowService extends Service {
    //借书
    async create(payload) {
        const{ctx}=this
        const temp = JSON.parse(JSON.stringify(payload))
        const res =await ctx.model.Borrow.create(temp);
        this.ctx.body=res
    }
    //查看借书情况
    async find(name){
        const res=await this.ctx.model.Borrow.find({name:{$regex:name.name}});
        return (this.ctx.body=res);
      }
    //归还图书
    async delete(_id){
        let res;
        const{ctx}=this;
        try{
            res=await ctx.model.Borrow.findById(_id);
            if(res){
                res=await ctx.model.Borrow.findByIdAndRemove(_id);
                return ctx.body=res;
               
            }
            return (ctx.body='用户不存在或者未输入ID');
        }catch(error){
            return error;
        }
    }
 //书名查找
    async findname(bookname){
        console.log(bookname);
        const res=await this.ctx.model.Borrow.find({bookname:{$regex:bookname.bookname}});
        // const res=await this.ctx.model.Book.find();
        return (this.ctx.body=res);
    }
    async f(_id){
        const res=await this.ctx.model.Borrow.findById(_id);
        return (this.ctx.body=res);
    }
}

module.exports = BorrowService;
